package cn.edu.buaa.neo.query.cxx.misuse.missingProblem.functionArgs;

import cn.edu.buaa.neo.codegraphql.core.graph.values.Value;
import cn.edu.buaa.neo.codegraphql.core.graph.values.statements.expressions.CallExpression;
import cn.edu.buaa.neo.codegraphql.dsl.fluent.query.QueryDescriptor;
import cn.edu.buaa.neo.codegraphql.dsl.lib.engine.QueryEngine;
import cn.edu.buaa.neo.codegraphql.language.cxx.CxxQuery;

import java.io.PrintStream;

public class OpenArgs extends CxxQuery {
    public static void main(String[] args) {
        QueryEngine.getInstance()
                .execute(OpenArgs.class.getSimpleName(), new OpenArgs())
                .close();
    }

    @Override
    public void run(String style, PrintStream output) {
        QueryDescriptor.open()
                .from("source", value -> value.toStream().anyMatch(this::valuePre))
                .select("source")
                .display();
    }

    private Boolean valuePre(Value value) {
        if (value instanceof CallExpression) {
            return "open".equals(((CallExpression) value).getFunction().getName())
                    && ((CallExpression) value).getArgumentsSize() <= 2;
        }
        return false;
    }
}
